﻿using SYH.Common.Utilities;
using SYH.Domain.Applicaton;
using SYH.IBLL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SYH.Web.Controllers.Apis.Application
{
    public class IMenuController : BaseController
    {
        private readonly IEntityRepository<ApplicationMemu> _MenuService;
        public IMenuController(IEntityRepository<ApplicationMemu> MenuService)
        {
            _MenuService = MenuService;
        }

        // GET: IMenu
        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            UserInfo bean = new UserInfo();
            bean.realName = LogUser.ChineseName;
            bean.psessionid = LogUser.ID.ToString();

            Hashtable hash = new Hashtable();
            var list = EachMenuRole(null);
            if ((list == null || list.Count == 0) && LogUser.GroupCode == GroupCode.Administrator)
            {
                list = new List<Menu>();
                Menu menu = new Menu();
                menu.menuId = Guid.NewGuid().ToString();
                menu.menuName = "导入菜单";
                menu.target = "VMenu";
                Menu menu2 = new Menu();
                menu2.menuId = Guid.NewGuid().ToString();
                menu2.menuName = "导入数据";
                menu2.target = "VImport";
                list.Add(menu);
                list.Add(menu2);
            }
            list = SetUCenter(list);
            hash["version"] = "";
            hash["menu"] = list;
            hash["userinfo"] = bean;
            hash["config"] = new { pcUrl = "/", logoutUrl = "VHome/LoginOut", search = false };

            return JsonData(hash);
        }
        /// <summary>
        /// 获取全部菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMenuTree()
        {
            var list = EachMenu(null);
            return JsonData(list);
        }

        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRoleMenuTree()
        {
            var list = EachMenuRole(null);
            return JsonData(list);
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(Guid id)
        {
            _MenuService.DeleteBo(id);
            CacheExtend.Remove("ApplicationMemu");
            return Success();
        }

        /// <summary>
        /// 迭代获取菜单树(权限)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<Menu> EachMenuRole(string id)
        {
            var rid = LogUser.RID;
            var gid = LogUser.GroupCode;
            List<Menu> list = null;
            List<ApplicationMemu> mlist = null;
            IList<ApplicationMemu> alist = null;//需要使用缓存

            alist = _MenuService.GetBoCollection().OrderByDescending(o => o.SortCode).ToCache();
            List<Guid> rlist = null;//权限菜单
            //if (rid == Guid.Empty)
            //{
            //    rlist = _GRMService.GetBoCollection(w => w.Code == gid).ToCache("GroupCode_" + gid).Select(s => s.TID).ToList();//权限菜单
            //}
            //else
            //{
            //    rlist = _RMService.GetBoCollection(w => w.RID == rid).ToCache(rid.ToString()).Select(s => s.MID).ToList();//权限菜单
            //}
            rlist = alist.Select(s => s.ID).ToList();

            if (string.IsNullOrEmpty(id) && null != alist)
            {
                mlist = alist.Where(w => string.IsNullOrEmpty(w.ParentId)).OrderBy(o => o.SortCode).ToList();
            }
            else if (null != alist)
            {
                mlist = alist.Where(w => w.ParentId == id).OrderBy(o => o.SortCode).ToList();
            }
            if (mlist != null && mlist.Count > 0)
            {
                list = new List<Menu>();
                if (rlist != null)
                {
                    foreach (var item in mlist)
                    {
                        var has = rlist.Where(w => w == item.ID).Count();
                        if (has > 0)
                        {
                            Menu menu = new Menu();
                            menu.menuId = item.ID.ToString();
                            menu.menuName = item.Name;
                            menu.target = item.Target;
                            menu.menuicon = item.CssIcon;
                            menu.sortCode = item.SortCode;
                            menu.childs = EachMenuRole(item.ID.ToString());
                            list.Add(menu);
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 迭代获取菜单树
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<Menu> EachMenu(string id)
        {
            List<Menu> list = null;
            List<ApplicationMemu> mlist = null;
            IList<ApplicationMemu> alist = null;//需要使用缓存
            alist = _MenuService.GetBoCollection().OrderByDescending(o => o.SortCode).ToCache();
            if (string.IsNullOrEmpty(id) && null != alist)
            {
                mlist = alist.Where(w => string.IsNullOrEmpty(w.ParentId)).OrderBy(o => o.SortCode).ToList();
            }
            else if (null != alist)
            {
                mlist = alist.Where(w => w.ParentId == id).OrderBy(o => o.SortCode).ToList();
            }
            if (mlist != null && mlist.Count > 0)
            {
                list = new List<Menu>();
                foreach (var item in mlist)
                {
                    Menu menu = new Menu();
                    menu.menuId = item.ID.ToString();
                    menu.menuName = item.Name;
                    menu.target = item.Target;
                    menu.menuicon = item.CssIcon;
                    menu.sortCode = item.SortCode;
                    menu.IsAction = item.IsAction;
                    menu.Weight = item.Weight;
                    menu.childs = EachMenu(item.ID.ToString());
                    list.Add(menu);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<Menu> SetUCenter(List<Menu> list)
        {
            Menu menu = new Menu();
            menu.menuId = Guid.NewGuid().ToString();
            menu.menuName = "框架样例";
            menu.menuicon = "icon-edit";
            menu.childs = new List<Menu>();
            Menu etbean = new Menu();
            etbean.menuId = Guid.NewGuid().ToString();
            etbean.menuName = "框架样例";
            etbean.target = "VExample";
            menu.childs.Add(etbean);
            list.Add(menu);
            return list;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="bean"></param>
        /// <returns></returns>
        public ActionResult SaveMenu(ApplicationMemu bean)
        {

            var bo = _MenuService.GetBo(bean.ID);
            if (bo == null)
            {
                _MenuService.SaveBo(bean);
            }
            else
            {
                bo.Name = bean.Name;
                //bo.ParentId = bean.ParentId;
                bo.SortCode = bean.SortCode;
                bo.Target = bean.Target;
                bo.CssIcon = bean.CssIcon;
                bo.IsAction = bean.IsAction;
                bo.Weight = bean.Weight;
                _MenuService.SaveBo(bo);
            }
            CacheExtend.Remove("ApplicationMemu");
            return Success();
        }

        public class Menu
        {
            public string menuId { get; set; }

            public string menuName { get; set; }

            public string parentId { get; set; }

            public string target { get; set; }

            public string menuicon { get; set; }

            public string sortCode { get; set; }
            public int Weight { get; set; }
            public bool IsAction { get; set; }

            public List<Menu> childs { get; set; }
        }

        public class UserInfo
        {
            public string realName { get; set; }

            public string psessionid { get; set; }
        }
    }
}